<?php 
include_once("../includes/page_utf_class.php");
include("../lang/cn/company_type_config.php");
include_once("../module/product/includes/plugin_tarento_class.php");
$tarento = new Tarento();

if($_GET['act']=='preparation_for_settlement'){//完善合伙人数据
    $monthStr = date('Ym',strtotime('-1 month'));
    $tarento->preparation_for_settlement($monthStr);
    
    msg('module.php?m=product&s=settlement_records.php');
}

if($_GET['act']=='settle_last_month'){
	$ymd = date('Ymd');
	$ymdf = date('Ym01');
	$ymds = date('Ym07');
	if($ymd>=$ymdf && $ymd<=$ymds){
		echo "8号之前不允许结算上月返利";
		exit();
	}
	echo "结算中……请稍候!<br />";
	$res = $tarento->settle_last_month_rebate();
	if($res['code']==1){
		echo $res['msg'];exit;
	}
	echo "结算完成!<br />";
	if(empty($res['result']['fail_general_member']) && empty($res['result']['fail_tarento'])){
		msg('module.php?m=product&s=settlement_records.php');
	}else{
		if($res['result']['fail_tarento']){
			echo "结算失败的合伙人用户ID:<br />";
			foreach ($res['result']['fail_tarento'] as $v)
			{
				echo $v."<br />";
			}	
		}
		if($res['result']['fail_general_member']){
			echo "结算失败的普通用户ID:<br />";
			foreach ($res['result']['fail_general_member'] as $vv)
			{
				echo $vv."<br />";
			}
		}
		exit();
	}
}
if($_GET['act']=='getTimeStrList'){
	$om = '201509';
	$nm = date('Ym');
	$res = array(array('time_str'=>$nm));
	$i = 1;
	while ($om<(date('Ym',strtotime('-'.$i.' month')))){
		$cm = date('Ym',strtotime('-'.$i.' month'));
		$res[] = array('time_str'=>$cm);
		$i++;
	}
	if($om != $nm){
		$res[] = array('time_str'=>$om);
	}
	echo json_encode($res);exit;
}

if($_GET['act'] && $_GET['act']=='ajax_get_records'){
	$list = array();
	if ($_POST['id'] > 0) {
		$sql = "select member_id,flow_time_str from ".TARENTOSALESFLOW." where id={$_POST['id']}";
		$db->query($sql);
		$minfo = $db->fetchRow();
		$list = $tarento->get_tarento_settlement_records($minfo['member_id'],$minfo['flow_time_str']);
	} else {
		if($_GET['member_id']>0){
			$list = $tarento->get_tarento_settlement_records($_GET['member_id'],$_GET['time_str'],TRUE);
		}else{
			$list = $tarento->get_tarento_settlement_records(0,$_GET['time_str']);
		}
	}
	foreach ($list as $k=>$v)
	{
		$sales = $tarento->get_tarento_current_month_sales($v['member_id'],$v['flow_time_str']);
		$list[$k]['member_num'] = $tarento->get_tarento_group_month_settlement_num($v['member_id'],$v['flow_time_str']);
		$list[$k]['state'] = $list[$k]['member_num']>1?'closed':'open';
		$list[$k]['name'] = empty($v['name'])?'匿名用户':$v['name'];
		$list[$k]['status_str'] = $v['status']?'已结算':'未结算';
		$list[$k]['group_sales'] = $sales['group_sales'];
		$list[$k]['formart_time'] = substr($v['flow_time_str'], 0, 4)."年".substr($v['flow_time_str'], 4)."月";
		if($_POST['id'] > 0){
			$list[$k]['parentId'] = $_POST['id'];
		}
	}
	
	echo json_encode($list);exit;
}

$str = "";
if($_GET['member_id']>0){
	$str .= "and a.member_id={$_GET['member_id']}";
}
$str .= " and a.relations!=''";
// if($_GET['member_type']=='1'){//合伙人
// 	$str .= " and a.relations!=''";
// }elseif($_GET['member_type']=='2'){//普通用户
// 	$str .= " and a.relations=''";
// }
if($_GET['time_str']){
	$str .= " and a.flow_time_str='{$_GET['time_str']}'";
}

$sql = "select a.*,b.name from ".TARENTOSALESFLOW." as a left join ".MEMBER." as b on a.member_id=b.userid where 1 {$str}";
//=============================
$page = new Page;
$page->listRows=20;
if (!$page->__get('totalRows')){
	$db->query($sql);
	$de['count']=$page->totalRows = $db->num_rows();
}
$de['count']=$de['count']?$de['count']:$_GET['totalRows'];
$sql .= "  limit ".$page->firstRow.",".$page->listRows;
$de['page'] = $page->prompt();
//=============================
$db->query($sql);
$ss=$db->getRows();
foreach($ss as $k)
{
	$sales = $tarento->get_tarento_current_month_sales($k['member_id'],$k['flow_time_str']);
	$k['group_sales'] = $sales['group_sales'];
	$k['formart_time'] = substr($k['flow_time_str'], 0, 4)."年".substr($k['flow_time_str'], 4)."月";
	$list[]=$k;
}
$de['list'] = $list;

/******是否能完善上月合伙人数据********/
$ymd = date('Ymd');
$ymdf = date('Ym01');
$ymds = date('Ym07');
$isShowImproveButton = 0;
// if($ymd>=$ymdf && $ymd<=$ymds){
//     $isShowImproveButton = 1;
// }
/***************/

$tpl->assign("de",$de);
$tpl->assign("isShowImproveButton",$isShowImproveButton);
$tpl->assign("config",$config);
$tpl->display("settlement_records.htm");
?>